{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-07-26T17:40:28.577480Z",
     "start_time": "2023-07-26T17:40:28.524400Z"
    }
   },
   "outputs": [],
   "source": [
    "import pickle"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-07-26T17:40:29.773085Z",
     "start_time": "2023-07-26T17:40:28.531132Z"
    }
   },
   "outputs": [],
   "source": [
    "# add path\n",
    "import sys\n",
    "sys.path.append('..')\n",
    "import os\n",
    "# print(os.cwd())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/shibo/anaconda3/envs/rap/lib/python3.10/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
      "  from .autonotebook import tqdm as notebook_tqdm\n"
     ]
    }
   ],
   "source": [
    "from reasoners.visualization import visualize"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-07-26T17:44:32.732062Z",
     "start_time": "2023-07-26T17:44:32.477654Z"
    }
   },
   "outputs": [],
   "source": [
    "from reasoners.visualization.tree_snapshot import NodeData\n",
    "from reasoners.algorithm.mcts import MCTSNode"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "mcts_result = pickle.load(open('/home/shibo/LLM-search/logs/gsm8k_MCTS/07282023-143709/algo_output/1.pkl', 'rb'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "def gsm_node_data_factory(x: MCTSNode):\n",
    "    if not x.state:\n",
    "        return {}\n",
    "    return {\"question\": x.state[-1].sub_question, \"answer\": x.state[-1].sub_answer}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Visualizer URL: https://www.llm-reasoners.net/visualizer/966bb51b-7626-452e-9f1f-402467f3902f?accessKey=a05e7703\n"
     ]
    }
   ],
   "source": [
    "# This line of code is compatible with the current version of demo.\n",
    "visualize(mcts_result, node_data_factory=gsm_node_data_factory)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "from reasoners.visualization import TreeLog"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "# os.mkdir(\"/home/shibo/LLM-search/logs/gsm8k_MCTS/07282023-143709/tree_logs\")\n",
    "for i in range(1, 21):\n",
    "    with open(f\"/home/shibo/LLM-search/logs/gsm8k_MCTS/07282023-143709/algo_output/{i}.pkl\", 'rb') as f:\n",
    "        mcts_result = pickle.load(f)\n",
    "    tree_log = TreeLog.from_mcts_results(mcts_result, node_data_factory=gsm_node_data_factory)\n",
    "    with open(f\"/home/shibo/LLM-search/logs/gsm8k_MCTS/07282023-143709/tree_logs/tree_log_{i}.json\", 'w') as f:\n",
    "        f.write(str(tree_log))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Found cached dataset gsm8k (/home/shibo/.cache/huggingface/datasets/gsm8k/main/1.1.0/37bfb08b1d4fcbb01f06b03d9e1ef5f1fcbd4d3af3d08842c50d7305091285ba)\n"
     ]
    }
   ],
   "source": [
    "import datasets\n",
    "import json\n",
    "questions = []\n",
    "# load gsm8k\n",
    "data = datasets.load_dataset(\"gsm8k\", \"main\", split=\"test\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "json.dump(data[:20][\"question\"], open(\"gsm_questions.json\", \"w\"))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "rap",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.11"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
